package com.applet.config;

import com.applet.common.json.JacksonObjectMapper;
import com.applet.interceptor.JwtTokenInterceptor;
import com.applet.interceptor.PermissionInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.io.File;
import java.util.List;

/**
 * @author “XuShao”
 * @date 2024/12/16 22:02
 * @description
 */
@Configuration
@Slf4j
public class WebMvcConfig {
    // 本地上传
//    @Value("${zyz.file.image-path}")
//    private String imagePath;
//
//    @Value("${zyz.file.video-path}")
//    private String videoPath;

    @Autowired
    private JwtTokenInterceptor jwtTokenInterceptor;

//    @Autowired
//    private PermissionInterceptor permissionInterceptor;

    @Bean
    public WebMvcConfigurer webMvcConfigurer() {
        return new WebMvcConfigurer() {
            // 本地上传
//            @Override
//            public void addResourceHandlers(ResourceHandlerRegistry registry) {
//                registry.addResourceHandler("/image/**").addResourceLocations("file:" + imagePath + File.separator);
//                registry.addResourceHandler("/video/**").addResourceLocations("file:" + videoPath + File.separator);
//            };

            @Override
            public void addInterceptors(InterceptorRegistry registry) {
                log.info("开始注册自定义拦截器...");
                registry.addInterceptor(jwtTokenInterceptor)
                        .addPathPatterns("/**")
                        .excludePathPatterns("/swagger-resources/**","/swagger-ui/**", "/api-docs/**", "/volunteer/login", "/volunteer/login2")
                        ;

//                registry.addInterceptor(permissionInterceptor)
//                        .addPathPatterns("/admin/**")
//                        .order(1);
            }

//            @Override
//            public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
//                log.info("扩展消息转换器...");
//                //创建一个消息转换器对象
//                MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
//                //需要为消息转换器设置一个对象转换器，对象转换器可以将Java对象序列化为json数据
//                converter.setObjectMapper(new JacksonObjectMapper());
//                //将自己的消息转化器加入容器中
//                converters.add(0,converter);
//            }
        };
    }
}
